<template>
  <div>
    <div>
      <Row class="header" type="flex" justify="start">
        <Col span="24">
          <img class="icon" src='@/assets/images/pie2.png'/>
          <span class="title">社工人数统计柱形图</span>
        </Col>
      </Row>
      <br>
      <div id="personSumBarEchart" ref="personSumBarEchart" :style="{ width: width + 'px', height: height + 'px'}"></div>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import {off, on} from "@/libs/tools";
export default {
  name: 'PersonSumBarEchart',
  data () {
    return {
      myChart:null,
      chartData: []
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 300
      }
    }
  },
  mounted () {
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  },
  methods: {
    resize () {
      console.log('屏幕尺寸发生bar变化')
      this.myChart.resize()
    },
    chart (data) {
      this.chartData = data
      let option = {
        title: {
          text: ''
        },
        textStyle: {
          color: '#bababa'
        },
        legend: {
          x:'10%',
          left: 'left',
          itemWidth:15,
          itemHeight:15,
          itemStyle:{
            color:'#1A5FFF'
          }
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(50,50,50,0.6)',
          // axisPointer: { // 坐标轴指示器，坐标轴触发有效
          //   type: 'bar' // 默认为直线，可选为：'line' | 'shadow'
          // }
        },
        dataset: {
          dimensions: ['region', 'number'],
          source: []
        },
        yAxis: {
          name:'单位:人',
          nameLocation:'end',
          nameTextStyle:{
            padding:[0,0,0,1500]
          },
          splitLine: {
            show: true,
            lineStyle:{
              color:'#F1F1F1'
            }
          },
          type: 'value',
          axisLine: {
            lineStyle: {
              color: 'transparent' // 轴及其文字颜色
            }
          },
          textStyle: {
            color: '#bababa' // 轴及其文字颜色

          }
        },
        xAxis: {
          boundaryGap:true,
          type: 'category',
          axisLabel: {
            interval: 0,
            formatter: function (params) {
              let newParamsName = ''; // 拼接后的新字符串
              let paramsNameNumber = params.length; // 实际标签数
              let provideNumber = 3; // 每行显示的字数
              let rowNumber = Math.ceil(paramsNameNumber / provideNumber); // 如需换回，算出要显示的行数

              if (paramsNameNumber > provideNumber) {
                /** 循环每一行,p表示行 */

                for (var i = 0; i < rowNumber; i++) {
                  var tempStr = ''; // 每次截取的字符串
                  var start = i * provideNumber; // 截取位置开始
                  var end = start + provideNumber; // 截取位置结束

                  // 最后一行的需要单独处理

                  if (i == rowNumber - 1) {
                    tempStr = params.substring(start, paramsNameNumber);
                  } else {
                    tempStr = params.substring(start, end) + '\n';
                  }
                  newParamsName += tempStr;
                }
              } else {
                newParamsName = params;
              }

              return newParamsName;
            }
          },
          axisLine: {
            show:false,
            lineStyle: {
              color: '#bababa' // 轴及其文字颜色
            },
            textStyle:{
              color: '#bababa' // 轴及其文字颜色
            }
          },
        },
        grid: {
          y: 50,
          y2: 100,
          left: '10%',
          bottom:'10%'
        },
        series: [
          {
            x:'center',
            y:'25%',
            name: '社工人数',
            type: 'bar',
            barMinWidth: '27',
            // barCategoryGap:'60%',
            label: {
              normal: {
                show: true,
                position: 'top',
                color: ['#1A5FFF'],
              }
            },
            itemStyle: {
              normal: {
                barBorderRadius: 5,
                color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  { offset: 1, color: '#1A5FFF' },
                  { offset: 0.5, color: '#188df0' },
                  { offset: 0, color: '#FFFFFF' }
                ])
              },

            // 通常情况下：
            //   normal: {
            //     // color: ['#1A5FFF'],
            //
            //   },
              // 鼠标悬停时：
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      this.chartData.forEach(item => {
        // area为空的是之前导入的数据（暂不显示）
        if (item.region) {
          option.dataset.source.push(item)
        }
      })
       this.myChart = echarts.init(document.getElementById('personSumBarEchart'))
      this.myChart.setOption(option)
      on(window, 'resize', this.resize)
      // console.log(option)
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    /*font-size: 28px;*/
    /*font-weight: 600;*/
    margin-right: 5px;
  }

  .title {
    font-size: 14px;
    text-align: left;
    color: #161616;
  }
</style>
